package com.ainote.controller;



import com.ainote.service.ExportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.util.Map;

@RestController
@RequestMapping("/api/export")
public class ExportController {

    @Autowired
    private ExportService exportService;

    @PostMapping("/word")
    public ResponseEntity<FileSystemResource> exportWord(@RequestBody Map<String, String> body) throws Exception {
        String title = body.getOrDefault("title", "会议纪要");
        String summary = body.getOrDefault("summary", "");
        String text = body.getOrDefault("text", "");

        File file = exportService.exportWord(title, summary, text);
        return buildResponse(file, "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
    }

    @PostMapping("/pdf")
    public ResponseEntity<FileSystemResource> exportPdf(@RequestBody Map<String, String> body) throws Exception {
        String title = body.getOrDefault("title", "会议纪要");
        String summary = body.getOrDefault("summary", "");
        String text = body.getOrDefault("text", "");

        File file = exportService.exportPdf(title, summary, text);
        return buildResponse(file, "application/pdf");
    }

    private ResponseEntity<FileSystemResource> buildResponse(File file, String mimeType) {
        FileSystemResource resource = new FileSystemResource(file);
        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + file.getName())
                .contentType(MediaType.parseMediaType(mimeType))
                .body(resource);
    }
}
